import { Component, OnInit, Output, EventEmitter, Input, ViewChild } from '@angular/core';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { Service } from '../../../server/server';
import { template } from '../../product.data';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { InitService } from '../../../public/serve/init/init.service';
import { DatePipe } from '@angular/common';

declare var $: any;

@Component({
  selector: 'app-template-add',
  templateUrl: './template-add.component.html',
  styleUrls: ['./template-add.component.css']
})
export class TemplateAddComponent implements OnInit {

  @Output() onSubmit = new EventEmitter<void>();
  @ViewChild('addForm') formElement: HTMLFormElement;
  @Input() themeTagListVal: any;

  checkStatus : number = 0; //默认是未审核
  keywordListVal : Array<object>;

  showAddChild: any = 0;

  constructor(
    private server: Service,
    public initService: InitService,
    private request: CudTableService,
    private hintsService: HintsService,
    private datePipe: DatePipe
  ) { }

  ngOnInit() {
  }

  /**
   * 新增提交
   * @param info
   */
  _onSubmit(info:any){

    info.token = template.retrieval.token;
    info.createBy = this.server.getuserName();
    this.request
    .postRequest(this.server.baseUrl +"Service/Config/addMetaDataTemplate",info)
    .then(res =>{
      if(res.result_code === "0000"){
        this.onSubmit.emit();
      }

      this.hintsService.messages(res.message,res.result_code);
      this._initAdd();
      return;
    })
    .catch(err =>{
      this.hintsService.messages("error","error");
      this._initAdd();
      return;
    });
  }

   /**
    * 初始化
    */
   private _initAdd(){
    this.initService._init();
    this.formElement.reset();
    $("#templateAdd").modal("hide");
   }

}
